package com.laicunba.ext.web.controller.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.lang.time.DateUtils;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.pojo.Page;
import com.laicunba.ext.pojo.PositionConditionVO;
import com.laicunba.ext.service.SlaveFinancingTargetService;
import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/webapi/ext")
public class FinancingTargetFindbyPositionAPIController extends BaseController {
	@Autowired
	private SlaveFinancingTargetService slaveFinancingTargetService;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "/financing_target_findby_position.htm", method=RequestMethod.GET)
	public String financingTargetFindbyPosition(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		String callback = request.getParameter("callback");
		String position = request.getParameter("position");
		if(StringUtil.isEmpty(callback) || StringUtil.isEmpty(position)) {
			json.put("code", ResponseCode.PARAM_ERROR);
			json.put("page", null);
			json.put("data", null);
			return callback + "(" + toJsonObject(json) + ")";
		}
		
		String limit = request.getParameter("limit");
		String pageIndex = request.getParameter("pageIndex");
		String sortColumn = request.getParameter("sortColumn");
		String sortStyle = request.getParameter("sortStyle");
		if (!StringUtil.isNumber(limit)) {
			limit = "10";
		}
		if(!StringUtil.isNumber(pageIndex) || Integer.parseInt(pageIndex) <= 0) {
			pageIndex = "1";
		}
		if(StringUtil.isEmpty(sortColumn)) {
			sortColumn = "";
		}
		if(StringUtil.isEmpty(sortStyle)) {
			sortStyle = "";
		}
		
		String targetListJson = CacheUtil.getAttribute("sys.target.position-" + position + ".sortColumn-" + sortColumn + ".sortStyle-" + sortStyle + ".pageIndex-" + pageIndex +".limit-" + limit);
		if(StringUtil.isNotEmpty(targetListJson)){
			return callback + "(" + targetListJson + ")";
		}
		
		List list = null;
		Page page = new Page(Integer.parseInt(pageIndex), Integer.parseInt(limit));
		if("WEB_REPAYMENT".equals(position)) {
			String cacheData = CacheUtil.getAttribute("sys.target.position-APP_REPAYMENT_LIST." + DateUtil.formatDate(new Date()));
			if(StringUtil.isEmpty(cacheData)) {
				cacheData = CacheUtil.getAttribute("sys.target.position-APP_REPAYMENT_LIST." + DateUtil.formatDate(DateUtils.add(new Date(), Calendar.DATE, -1)));
			}
			
			JSONArray repaymentArray = JSONArray.fromObject(cacheData);
			page.setTotalCount(repaymentArray.size());
			if(repaymentArray != null && repaymentArray.size() > 0) {
				list = new ArrayList();
				for(int i = page.getFirstResult() - 1; i < page.getPageIndex() * page.getPageSize() && i < page.getTotalCount(); i++) {
					list.add(repaymentArray.get(i));
				}
			}
		} else {
			PositionConditionVO conditionVO = new PositionConditionVO();
			conditionVO.setPosition(position);
			conditionVO.setSortColumn(sortColumn);
			conditionVO.setSortStyle(sortStyle);
			conditionVO.setLimit(Integer.valueOf(limit));
			conditionVO.setOffset((Integer.valueOf(pageIndex) - 1) * (Integer.valueOf(limit)));
			page.setTotalCount(slaveFinancingTargetService.countFinancingTargetByPosition(conditionVO));
			list = slaveFinancingTargetService.findFinancingTargetByPosition(conditionVO);
		}
		
		if (list == null || list.isEmpty()) {
			json.put("code", ResponseCode.SUCCESS);
			json.put("page", page);
			json.put("data", null);
		} else {
			json.put("code", ResponseCode.SUCCESS);
			json.put("page", page);
			json.put("data", list);
		}
		targetListJson = toJsonObject(json);
		CacheUtil.setAttribute("sys.target.position-" + position + ".sortColumn-" + sortColumn + ".sortStyle-" + sortStyle + ".pageIndex-" + pageIndex +".limit-" + limit, targetListJson, 5);
		return callback + "(" + targetListJson + ")";
	}
}
